﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shapes
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Shape myshape;
            Console.WriteLine("请选择您要计算的形状(1：正方形；2：长方形；3：三角形；4：圆形)");
            int choice = int.Parse(Console.ReadLine());
            myshape = Factory.CreateShape(choice);
            Console.WriteLine("该形状面积为："+myshape.Area());
            Console.WriteLine("接下来请继续初始化10个对象：");
            Shape[] shapes;
            shapes = new Shape[10];
            for (int i = 0; i < shapes.Length; i++)
            {
                shapes[i] = Factory.CreateShape(i%4+1);
            }
            double sum = 0;
            foreach(Shape shape in shapes)
            {
                sum+=shape.Area();
            }
            Console.WriteLine("10个形状的总面积为："+sum);

            Console.Read();

        }
        public abstract class Shape
        {
            public abstract double Area();
            public abstract void Initialization();
        }
        class Square : Shape
        {
            double width;
            public Square(double n) { width = n; } 
            public Square()
            {
                Initialization();
            }
            public override double Area()
            {
                return Math.Pow(width, 2);
            }

            public override void Initialization()
            {
                while (true)
                {
                    Console.WriteLine("请输入正方形的边长：");
                    string widthStr = Console.ReadLine();
                    if (!double.TryParse(widthStr, out width))
                    {
                        Console.Write("输入数据出现非法字符，");
                        continue;
                    }
                    break;
                }
            }

        }
        class Rectangle : Shape
        {
            double width;
            double height;
            public Rectangle(double a,double b ) { width = a;height = b; }
            public Rectangle()
            {
                Initialization();
            }

            public override double Area()
            {
                return width * height;
            }

            public override void Initialization()
            {
                while (true)
                {
                    Console.WriteLine("请选择输入长方形的长：");
                    string widthStr = Console.ReadLine();
                    Console.WriteLine("请选择输入长方形的宽：");
                    string heightStr = Console.ReadLine();
                    if (!double.TryParse(widthStr, out width) || !double.TryParse(heightStr, out height))
                    {
                        Console.Write("输入数据出现非法字符，");
                        continue;
                    }
                    break;
                }
            }
        }
        class Triangle : Shape
        {
            double side1;
            double side2;
            double side3;
            public Triangle(double a,double b,double c) { side1 = a;side2 = b;side3 = c; }
            public Triangle()
            {
                Initialization();
            }

            public override double Area()
            {
                double p = (side1+side2 +side3)/2;
                return Math.Sqrt(p*(p-side1)*(p-side2)*(p-side3));
            }

            public override void Initialization()
            {
                while (true)
                {
                    Console.Write("请输入三角形第1个边长：");
                    string side1Str = Console.ReadLine();
                    Console.Write("请输入三角形第2个边长：");
                    string side2Str = Console.ReadLine();
                    Console.Write("请输入三角形第3个边长：");
                    string side3Str = Console.ReadLine();
                    if (!double.TryParse(side1Str, out side1) || !double.TryParse(side2Str, out side2)|| !double.TryParse(side3Str, out side3))
                    {
                        Console.WriteLine("您输入的数据出现非法字符，请重新输入！");
                        continue;
                    }
                    else if(!(((side1+side2)>side3)&&((side1 + side3) >= side2)&&((side3 + side2) >= side1)))
                    {
                        Console.WriteLine("您输入的数据无法构成三角形，请重新输入！");
                        continue;

                    }
                    break;
                }
            }
        }
        class Circle : Shape
        {
            double r;
            public Circle(double r) { this.r = r; }
            public Circle()
            {
                Initialization();
            }

            public override double Area()
            {
                return Math.PI * Math.Pow(r, 2);
            }


            public override void Initialization()
            {
                while (true)
                {
                    string result = "";
                    Console.WriteLine("请输入圆的半径：");
                    result = Console.ReadLine();
                    if (!double.TryParse(result, out r))
                    {
                        Console.Write("输入数字非法，");
                         continue;
                     }
                        break;
                    
                   
                    
                }
            }
        }
        class Factory
        {
            public static Shape CreateShape(int name)
            {
                switch (name)
                {
                    case 1: return new Square();
                    case 2: return new Rectangle();
                    case 3: return new Triangle();
                    case 4: return new Circle();
                    default:
                         Console.WriteLine("系统找不到您选择的形状！");
                        return null;
                }
            }
            
        }



    }
}
